package com.legal.aid.app.service.impl;

import com.legal.aid.app.domain.vo.CaseVo;
import com.legal.aid.app.mapper.AppCaseMapper;
import com.legal.aid.app.mapper.AppCollectMapper;
import com.legal.aid.app.service.IAppCaseService;
import com.legal.aid.common.constant.CollectType;
import com.legal.aid.common.core.domain.entity.CommonUser;
import com.legal.aid.common.core.domain.model.AppLoginUser;
import com.legal.aid.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class AppCaseServiceImpl implements IAppCaseService {

    @Autowired
    private AppCaseMapper appCaseMapper;

    @Autowired
    private AppCollectMapper appCollectMapper;

    @Override
    public List<CaseVo> selectCaseVo(CaseVo caseVo) {
        caseVo.setCaseStatus("1");
        return appCaseMapper.selectCaseVoList(caseVo);
    }

    @Override
    public CaseVo selectCaseByCaseId(Long caseId) {
        if(caseId == null ) {
            return null;
        }
        return appCaseMapper.getCaseDetailsById(caseId);
    }

    @Override
    public List<CaseVo> selectCaseByCaseTitle(String titleKey) {
        CaseVo caseVo = new CaseVo();
        caseVo.setCaseTitle(titleKey);
        List<CaseVo> list = appCaseMapper.selectCaseVoList(caseVo);
        return Optional.ofNullable(list)
                .filter(l -> !l.isEmpty())
                .orElse(Collections.emptyList());
    }

    @Override
    public List<CaseVo> selectAttentionCaseVoList() {
        AppLoginUser appLoginUser = SecurityUtils.getAppLoginUser();
        CommonUser user = appLoginUser.getCommonUser();
        if (user == null)
            return Collections.emptyList();
        
        List<Long> targetIds = appCollectMapper.getTargetIdList(CollectType.COLLECT_TYPE_CASE.toString(), user.getId());
        if (targetIds == null || targetIds.isEmpty())
            return Collections.emptyList();
        
        // 直接使用 List<Long> 类型
        List<CaseVo> list = appCaseMapper.selectCaseListByIds(targetIds);
        return Optional.ofNullable(list)
                .filter(l -> !l.isEmpty())
                .orElse(Collections.emptyList());
    }

}
